<template>
  <div>
    <el-tree
      :data="Perlist"
      v-model="PerInfo"
      show-checkbox
      node-key="value"
      ref="tree"
      default-expand-all
      :check-strictly="strictlyFlag"
    >
    </el-tree>
    <el-button type="primary" plain @click="Addper">保存</el-button>
    <!-- <el-button plain>全选</el-button>
    <el-button plain>取消全选</el-button> -->
  </div>
</template>

<script>
export default {
  props: {
    PerRoleid: null,
  },
  data() {
    return {
      Perlist: [],
      PerInfo: [],
      Pid: [],
      strictlyFlag: false,
    };
  },
  methods: {
    //树形
    TeerPer() {
      this.axios
        .get("http://localhost:15670/api/RoleUser/GetPerDto?id=0")
        .then((res) => {
          this.Perlist = res.data;
        });
    },
    //角色权限添加
    Addper() {
      this.Pid = this.$refs.tree
        .getHalfCheckedKeys()
        .concat(this.$refs.tree.getCheckedKeys());
      if (this.Pid.length == 0) {
        this.$message.error("至少选择一项权限");
        return;
      }
      var from = {
        RoleId: this.PerRoleid,
        PermissionId: this.Pid.toString(),
      };
      this.axios
        .post("http://localhost:15670/api/RoleUser/AddRolePer", from)
        .then((res) => {
          if (res.data.Success) {
            this.$message.success(res.data.Message);
            this.$emit("PreAdd");
          } else {
            this.$message.success(res.data.Message);
          }
        });
    },
    //角色权限获取修改
    GetPer() {
      this.axios
        .get(
          "http://localhost:15670/api/RoleUser/GetRolePermissionsDto?RoleId=" +
            this.PerRoleid
        )
        .then((res) => {
          var ids = [];
          res.data.Data.forEach((element) => {
            ids.push(element.PermissionId);
          });
          this.strictlyFlag = true;
          this.$nextTick(() => {
            this.$refs.tree.setCheckedKeys(ids);
          });
        });
    },
  },
  //加载
  created() {
    this.TeerPer();
    this.GetPer();
  },
};
</script>

<style>
</style>